#include "PaintVisitor.h"
#include "PointTranslator.h"
#include "../model/UMLBox.h"
#include "../model/UMLLine.h"

PaintVisitor::PaintVisitor(const PointTranslator& trans, QPainter& painter,
		const SelectionMan& selected,
		const std::set<UMLObject*>& ghosts)
	: _trans(trans), _painter(painter), _selected(selected), _ghosts(ghosts)
{
}

void PaintVisitor::visit(UMLBox& data)
{
	QRect rect = _trans.toscreen(data.rect());

	// We subtract (1,1) off the paint size because of Qt's rules for painting
	// rectangles.
	rect.adjust(0, 0, -1, -1);

	_painter.setPen(penColor(data));
	_painter.drawRect(rect);
	_painter.drawText(rect, Qt::AlignTop,
			data.text().fullText(), NULL); // TODO: real text
}

void PaintVisitor::visit(UMLLine& data)
{
	_painter.setPen(penColor(data));

	QSize grabSize = _trans.toscreen(QSize(1, 1));

	QPoint first = _trans.toscreen(data.vertex(0));

	// FIXME this is ugly
	if (_selected.contains(&data))
		_painter.drawRect(QRect(_trans.tocontrol(data.vertex(0)), grabSize));

	for (UMLLine::size_type index = 1; index < data.vertexCount(); ++index)
	{
		QPoint second = _trans.toscreen(data.vertex(index));

		_painter.drawLine(first, second);
		if (_selected.contains(&data))
			_painter.drawRect(QRect(
						_trans.tocontrol(data.vertex(index)), grabSize));

		first = second;
	}

	// TODO: more advanced line features
}

QColor PaintVisitor::penColor(UMLObject& obj)
{
	if (_selected.contains(&obj))
		return Qt::blue;

	if (_ghosts.find(&obj) != _ghosts.end())
		return Qt::gray;

	return Qt::black;
}

